<?php
/**
 * 广告管理
 * @author cjli
 *
 */
class AdvAction extends AdminAction
{
	 public function _initialize()
	 {
	 	parent::_initialize();
	 }
	 
	//轮播广告列表
	public function slide_list()
	{
		$slide_db = D('adv_slide');
		$slideList = $slide_db->order('listorder DESC')->select();
		$this->assign('slideList', $slideList);
		$this->display('slide_list');
	}
	
	//添加轮播广告
	public function slide_add()
	{
		if(isset($_POST['dosubmit'])){
		    if (isset($_FILES['upimage']) && $_FILES['upimage']['tmp_name']){
				$info = $this->upload('adv');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['img'] = UPLOAD_PATH . $info['upimage']['savename'];
					if(isset($_FILES['upimagem']) && $_FILES['upimagem']['tmp_name']){
					    $_POST['imgm'] = UPLOAD_PATH . $info['upimagem']['savename'];
					}
				}else{
					$this->error($info);
				}
			}elseif (isset($_FILES['upimagem']) && $_FILES['upimagem']['tmp_name']){
			    $info = $this->upload('adv');
			    if(is_array($info)) {// 上传成功 获取上传文件信息
			        $_POST['imgm'] = UPLOAD_PATH . $info['upimagem']['savename'];
			    }else{
			        $this->error($info);
			    }
			}
			$slide_db = D('adv_slide');
			$slide_db->create();
			$lastId = $slide_db->add();
			if($lastId){
				S('slide_list', NULL);
				$this->success('轮播广告编辑成功！', U('adv/slide_list'), 'add');
			}else{
				$this->error('轮播广告编辑失败！', U('adv/slide_list'), 'add');
			}
		}else{
			$this->display('slide_add');
		}
	}
	
	//编辑轮播广告
	public function slide_edit()
	{
		$sid = intval($_GET['sid']);
		$slide_db = D('adv_slide');
		$slideInfo = $slide_db->find($sid);
		if(!$slideInfo){
			$this->error('轮播广告不存在！', U('adv/slide_list'), 'edit');
		}
		if(isset($_POST['dosubmit'])){
			if (isset($_FILES['upimage']) && $_FILES['upimage']['tmp_name']){
				$info = $this->upload('adv');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['img'] = UPLOAD_PATH . $info['upimage']['savename'];
					if(isset($_FILES['upimagem']) && $_FILES['upimagem']['tmp_name']){
					    $_POST['imgm'] = UPLOAD_PATH . $info['upimagem']['savename'];
					}
				}else{
					$this->error($info);
				}
			}elseif (isset($_FILES['upimagem']) && $_FILES['upimagem']['tmp_name']){
			    $info = $this->upload('adv');
			    if(is_array($info)) {// 上传成功 获取上传文件信息
			        $_POST['imgm'] = UPLOAD_PATH . $info['upimagem']['savename'];
			    }else{
			        $this->error($info);
			    }
			}
			$slide_db->create();
			$slide_db->where('id='. $sid)->save();
			S('slide_list', NULL);
			$this->success('轮播广告编辑成功！', U('adv/slide_list'), 'edit');
		}else{
			$this->assign('slide', $slideInfo);
			$this->display('slide_edit');
		}
	}
	//删除轮播广告
	public function slide_delete()
	{
		$sid = intval($_GET['sid']);
		$slide_db = D('adv_slide');
		$slide_db->delete($sid);
		S('slide_list', NULL);
		$this->success('轮播广告删除成功！', U('adv/slide_list'));
	}
	
	//广告位置列表
	public function position_list()
	{
		$position_db = D('adv_position');
		$positionList = $position_db->select();
		$this->assign('positionList', $positionList);
		$this->display('position_list');
	}
	
	//添加广告位
	public function position_add()
	{
		if(isset($_POST['dosubmit'])){
			$position_db = D('adv_position');
			$_POST['ad_nums'] = $_POST['ad_nums'] ? intval($_POST['ad_nums']) : 1;
			$position_db->create();
			$lastId = $position_db->add();
			if($lastId){
				S('position_list', NULL);
				$this->success('广告位添加成功！', U('adv/position_list'), 'add');
			}else{
				$this->error('广告位添加失败！', U('adv/position_list'), 'add');
			}
		}else{
			$this->display('position_add');
		}
	}
	//编辑广告位
	public function position_edit()
	{
		$pid = intval($_GET['pid']);
		$position_db = D('adv_position');
		$positionInfo = $position_db->find($pid);
		if(!$positionInfo){
			$this->error('广告位不存在！', U('adv/position_list'), 'edit');
		}
		if(isset($_POST['dosubmit'])){
			$_POST['ad_nums'] = $_POST['ad_nums'] ? intval($_POST['ad_nums']) : 1;
			$position_db->create();
			$position_db->where('id='.$pid)->save();
			S('position_list', NULL);
			$this->success('广告位编辑成功！', U('adv/position_list'), 'edit');
		}else{
			$this->assign('position', $positionInfo);
			$this->display('position_edit');
		}
	}
	//删除广告位
	public function position_delete()
	{
		$pids = $_POST['pids'];
		if($pids){
			$pid = join(',', $pids);
		}else{
			$pid = intval($_GET['pid']);
		}
		$position_db = D('adv_position');
		$position_db->delete($pid);
		S('position_list', NULL);
		$this->success('广告位删除成功！', U('adv/position_list'));
	}
	
	//广告列表
	public function adv_list()
	{
		$adv_db = D('adv_manage');
		$count = $adv_db->count();//总记录数
		import("ORG.Util.Page");// 导入分页类
		$Page = new Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数
		$pageShow = $Page->show();// 分页显示输出
		$this->assign('pageShow', $pageShow);
		
		$adv_db->field('adv_manage.id,adv_manage.name,adv_manage.ad_type,adv_manage.width,adv_manage.height,adv_manage.listorder,adv_manage.start_time,adv_manage.end_time,adv_position.name AS position_name,adv_manage.content');
		$adv_db->order('adv_manage.listorder DESC');
		$adv_db->join('adv_position ON adv_position.id = adv_manage.position_id');
		$adv_db->limit($Page->firstRow.','.$Page->listRows);
		$advList = $adv_db->select();
		$this->assign('advList', $advList);
		$advType = AdvModel::showType();
		$this->assign('advType', $advType);
		$this->display('adv_list');
	}
	
	//添加广告
	public function adv_add()
	{
		if(isset($_POST['dosubmit'])){
			$ad_type = intval($_POST['ad_type']);
			if ($ad_type==1 && (isset($_FILES['img']) && $_FILES['img']['tmp_name'])){
				$info = $this->upload('adv');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['content'] = UPLOAD_PATH . $info['img']['savename'];
				}else{
					$this->error($info);
				}
			}elseif ($ad_type==2 && (isset($_FILES['flash']) && $_FILES['flash']['tmp_name'])){
				$info = $this->upload('adv', '', array('swf'));
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['content'] = UPLOAD_PATH . $info['flash']['savename'];
				}else{
					$this->error($info);
				}
			}elseif($ad_type==3){
				$_POST['content'] = $_POST['text'];
			}elseif($ad_type==4){
				$_POST['content'] = $_POST['code'];
			}
			$adv_db = D('adv_manage');
			$adv_db->create();
			$lastId = $adv_db->add();
			if($lastId){
				S('adv_list', NULL);
				S('position-'.$_POST['position_id'], NULL);
				$this->success('广告添加成功！', U('adv/adv_list'));
			}else{
				$this->error('广告添加失败！', U('adv/adv_list'));
			}
		}else{
			$positionList = AdvModel::getPositionList();
			$this->assign('positionList', $positionList);
//			$advType = AdvModel::showType();
//			$this->assign('advType', $advType);
			$this->display('adv_add');
		}
	}
	
	//编辑广告
	public function adv_edit()
	{
		$aid = intval($_GET['aid']);
		$adv_db = D('adv_manage');
		$advInfo = $adv_db->find($aid);
		if(!$advInfo){
			$this->error('广告不存在！', U('adv/adv_list'));
		}
		if(isset($_POST['dosubmit'])){
			$ad_type = intval($_POST['ad_type']);
			if ($ad_type==1 && (isset($_FILES['img']) && $_FILES['img']['tmp_name'])){
				$info = $this->upload('adv');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['content'] = UPLOAD_PATH . $info['img']['savename'];
				}else{
					$this->error($info);
				}
			}elseif ($ad_type==2 && (isset($_FILES['flash']) && $_FILES['flash']['tmp_name'])){
				$info = $this->upload('adv', '', array('swf'));
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['content'] = UPLOAD_PATH . $info['flash']['savename'];
				}else{
					$this->error($info);
				}
			}elseif($ad_type==3){
				$_POST['content'] = $_POST['text'];
			}elseif($ad_type==4){
				$_POST['content'] = $_POST['code'];
			}
			
			$_POST['type'] = $ad_type;
			$adv_db->create();
			$adv_db->where('id=' . $aid)->save();
			S('adv_list', NULL);
			S('position-'.$_POST['position_id'], NULL);
			$this->success('广告编辑成功！', U('adv/adv_list'));
		}else{
			$positionList = AdvModel::getPositionList();
			$this->assign('positionList', $positionList);
			$this->assign('adv', $advInfo);
			$this->display('adv_edit');
		}
	}
	
	//删除广告
	public function adv_delete()
	{
		$aids = $_POST['aids'];
		if($aids){
			$aid = join(',', $aids);
		}else{
			$aid = intval($_GET['aid']);
		}
		$adv_db = D('adv_manage');
		$advList = $adv_db->field('position_id')->where(array('id' =>array('in', $aid)))->group('position_id')->select();
		foreach ($advList as $adv){
			S('position-'.$adv['position_id'], NULL);
		}
		$adv_db->delete($aid);
		S('adv_list', NULL);
		
		$this->success('广告删除成功！', U('adv/adv_list'));
	}
	
	//修改友情链接排序
	public function public_slide_listorder_ajax()
	{
		$sid = intval($_GET['sid']);
		$listorder = intval($_GET['listorder']);
		$slide_db = D('adv_slide');
		$slide_db->where('id='.$sid)->save(array('listorder' => $listorder));
	}
}